/**************************************************
 * 上海美峰数码科技有限公司(http://www.morefuntek.com)
 * 模块名称: Merge
 * 功能描述：
 * 文件名：Merge.java
 **************************************************
 */
package com.ywh.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * 功能描述
 * 
 * @author yaowenhao
 * @since 2011-3-23
 * @version 1.0
 */
public class Merge {
	private Properties res = new Properties();
	private Properties zh_CN = new Properties();
	private static final String RES_PATH = "src/com/ywh/util/resource.properties";
	private static final String zh_CN_PATH = "src/com/ywh/util/messages_zh_CN.properties";
	private static final String out_xls = "src/com/ywh/util/added.xls";

	/**
	 * 功能描述
	 */
	private void load() {
		try {
			FileInputStream fis = new FileInputStream(RES_PATH);
			InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
			res.load(isr);
			fis = new FileInputStream(zh_CN_PATH);
			isr = new InputStreamReader(fis, "UTF-8");
			zh_CN.load(isr);
			isr.close();
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 功能描述
	 */
	private void merge() {
		Set<String> set = new HashSet<String>();
		int count = 0;
		for (Object obj : zh_CN.keySet()) {
			Object zhKey = zh_CN.get(obj);
			if (res.get(zhKey) != null) {
				// System.out.println(zhKey);
				count++;
			} else {
				set.add(zhKey.toString());
			}
		}
		System.out.println("已匹配：" + count + "，待匹配：" + set.size());
		exportExcel(set, out_xls);
	}

	public void testPrint() {
		System.out.println("已翻译资源条数：" + res.size());
		System.out.println("待翻译资源条数：" + zh_CN.size());
	}

	public void exportExcel(Set<String> set, String targetFile) {
		try {
			WritableWorkbook book = Workbook
					.createWorkbook(new File(targetFile));
			WritableSheet sheet = book.createSheet("补充", 0);
			int row = 0;
			for (String str : set) {
				sheet.addCell(new Label(0, row++, str));
			}
			book.write();
			book.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Merge m = new Merge();
		m.load();
		m.testPrint();
		m.merge();
	}
}
